package com.gh.controller.api;

import com.gh.exception.HospitalException;
import com.gh.helper.HttpRequestHelper;
import com.gh.model.hosp.Department;
import com.gh.model.hosp.Hospital;
import com.gh.model.hosp.Schedule;
import com.gh.result.Result;
import com.gh.result.ResultCodeEnum;
import com.gh.service.DepartmentService;
import com.gh.service.HospitalService;
import com.gh.service.HospitalSetService;
import com.gh.service.ScheduleService;
import com.gh.utils.MD5;
import com.gh.vo.hosp.DepartmentQueryVo;
import com.gh.vo.hosp.ScheduleQueryVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author Eric
 * @Date 2021/6/8 20:45
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/hosp")
public class Api1Controller {

    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    /**
     * 添加医院
     * @param request
     * @return
     */
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(requestMap);
        //获取医院系统传递过来的签名,签名进行了md5加密
        String hospSign = (String) map.get("sign");
        //根据传递过来的医院编号查询数据库,查询出签名
        String hoscode = (String) map.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if(!hospSign.equals(signKeyMd5)){
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        //传输过程中图片进行了base64编码,“+”转换成了“ ”，因此我们要转换回来
        String logoData = (String) map.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        map.put("logoData",logoData);
        //调用service加入数据库
        hospitalService.save(map);
        return Result.ok();
    }
    /**
     * 查询医院
     */
    @PostMapping("hospital/show")
    public Result hospitalShow(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //获取医院编号
        String hoscode = (String) map.get("hoscode");
        //获取医院系统传递过来的签名,签名进行了md5加密
        String hospSign = (String) map.get("sign");
        //判断签名是否一样
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if(!hospSign.equals(signKeyMd5)){
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service方法根据医院编号进行查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    /**
     * 上传科室
     * @return
     */
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String sign = (String) map.get("sign");
        //根据传递过来的医院编号查询数据库,查询出签名
        String hoscode = (String) map.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if(!sign.equals(signKeyMd5)){
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.save(map);
        return Result.ok();
    }

    /**
     * 分页显示科室信息
     * @param request
     * @return
     */
    @PostMapping("department/list")
    public Result departmentShow(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = (String) map.get("hoscode");
        Integer page = StringUtils.isEmpty((String) map.get("page")) ? 1 : Integer.parseInt((String) map.get("page"));
        Integer limit = StringUtils.isEmpty((String) map.get("limit")) ? 1 : Integer.parseInt((String) map.get("limit"));

        DepartmentQueryVo departmentQueryVo=new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //调用service方法根据医院编号进行查询
        Page<Department> pages = departmentService.getByHoscode(page,limit,departmentQueryVo);
        return Result.ok(pages);
    }

    /**
     * 删除科室
     */
    @PostMapping("department/remove")
    public Result deleteDepartment(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = (String) map.get("hoscode");
        String depcode = (String) map.get("depcode");
//        String sign = (String) map.get("sign");
//        //根据传递过来的医院编号查询数据库,查询出签名
//        String signKey = hospitalSetService.getSignKey(hoscode);
//        String signKeyMd5 = MD5.encrypt(signKey);
//        //判断签名是否一致
//        if(!sign.equals(signKeyMd5)){
//            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
//        }
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }
    /**
     * 上传排班
     */
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        scheduleService.save(map);
        return Result.ok();
    }

    /**
     * 分页显示科排班信息
     * @param request
     * @return
     */
    @PostMapping("schedule/list")
    public Result scheduleShow(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = (String) map.get("hoscode");
        Integer page = StringUtils.isEmpty((String)map.get("page"))?1:Integer.parseInt((String)map.get("page"));
        Integer limit = StringUtils.isEmpty((String)map.get("limit"))?1:Integer.parseInt((String)map.get("limit"));
        ScheduleQueryVo scheduleQueryVo=new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        Page<Schedule> pages=scheduleService.getByHoscode(page,limit,scheduleQueryVo);
        return Result.ok(pages);
    }
    /**
     * 删除排班
     */
    @PostMapping("schedule/remove")
    public Result deleteSchedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = (String) map.get("hoscode");
        String hosScheduleId = (String) map.get("hosScheduleId");
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }
}
